<template>
  <div class="app-container">
    <ViewSessions
      v-if="isinspection"
      class="sousuo"
      style="margin-bottom: 15px"
      @add="add"
    />
    <el-table
      :row-class-name="tableRowClassName"
      :data="outsourcingList"
      :header-row-style="{
        'background-color': '#E6E6E6',
        'font-weight': 'bold',
        color: '#333',
      }"
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
      height="650"
      style="width: 100%; margin-bottom: 20px"
      row-key="id"
    >
      <el-table-column show-overflow-tooltip align="center" prop="productCode" label="产品编号"/>
      <el-table-column show-overflow-tooltip align="center" prop="orderCode" label="订单编号"/>
      <el-table-column show-overflow-tooltip align="center" prop="manufacturer" label="外协厂商"/>
      <el-table-column show-overflow-tooltip align="center" prop="outTime" label="外协日期"/>
      <el-table-column show-overflow-tooltip align="center" prop="outOrderNo" label="外协订单号"/>

      <el-table-column show-overflow-tooltip align="center" prop="unitPrice" label="外协单价"/>
      <el-table-column show-overflow-tooltip align="center" prop="unitPriceExamine" label="外协单价审核"/>
      <el-table-column show-overflow-tooltip align="center" prop="deliveryTime" label="外协交期"/>
      <el-table-column show-overflow-tooltip align="center" prop="price" label="外协总价"/>

      <el-table-column show-overflow-tooltip align="center" label="是否交货">
        <template slot-scope="scope">
          <span>{{ scope.row.isDelivery == 1 ? '是' : '否' }}</span>
        </template>
      </el-table-column>

      <el-table-column show-overflow-tooltip align="center" label="是否备料">
        <template slot-scope="scope">
          <span>{{ scope.row.isStock == 1 ? '是' : '否' }}</span>
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip align="center" label="是否到货">
        <template slot-scope="scope">
          <span>{{ scope.row.isArrival == 1 ? '是' : '否' }}</span>
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip align="center" label="材料外发">
        <template slot-scope="scope">
          <span>{{ scope.row.materialOut == 1 ? '是' : '否' }}</span>
        </template>
      </el-table-column>
      <el-table-column width="120" show-overflow-tooltip align="center" label="外协进度">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.status ==1 && !isinspection"
            type="text"
            size="mini"
            @click="() => inspectionClick(scope.row)"
          >品检检测中
          </el-button>
          <span v-else>{{ scope.row.status ==0 ? '外协中' : (scope.row.status ==1 ? '品检检测中' : (scope.row.status ==2 ? '外协已完成(合格)' : (scope.row.status ==3 ? '不合格' : ''))) }}</span>
        </template>
      </el-table-column>
      <el-table-column v-if="isinspection" align="center" label="指派">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.status ==0"
            type="text"
            size="mini"
            @click="() => assigned(scope.row)"
          >指派
          </el-button>
        </template>
      </el-table-column>
      <el-table-column v-if="isinspection" fixed="right" align="center" label="操作">
        <template slot-scope="scope">
          <el-button
            type="text"
            size="mini"
            @click="() => upload(scope.row)"
          >修改
          </el-button>
          <el-button
            type="text"
            size="mini"
            @click="() => delect(scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :current-page="page"
      :total="total"
      :page-size="limit"
      style="padding: 30px 0; text-align: end"
      layout="total, prev, pager, next,jumper"
      @current-change="fetchData"
    />
    <el-dialog
      :visible.sync="dialogFormVisible"
      :title="dialogFormValue"
      @close="closeDialog"
    >
      <el-form
        ref="outsourcing"
        :model="outsourcing"
        :rules="validateRules"
        label-width="120px"
      >
        <!-- <el-form-item label="订单编号：" prop="orderCode">
          <el-col :span="8">
            <el-select v-model="outsourcing.orderCode" clearable placeholder="请选择订单" @change="changeorder">
              <el-option
                v-for="item in orderNoList"
                :key="item.insideCode"
                :label="item.insideCode"
                :value="item.insideCode"/>
            </el-select>
          </el-col>
        </el-form-item> -->
        <el-form-item label="外协厂商：" prop="name">
          <el-col :span="8">
            <el-input
              v-model="outsourcing.manufacturer"
              size="small"
              placeholder="请输入外协厂商"
            />
          </el-col>
        </el-form-item>
        <el-form-item label="外协日期：">
          <el-col :span="6">
            <el-date-picker
              v-model="outsourcing.outTime"
              value-format="yyyy-MM-dd"
              type="date"
              placeholder="选择外协日期"
            />
          </el-col>
        </el-form-item>

        <el-form-item label="外协订单号：" prop="name">
          <el-col :span="8">
            <el-input
              v-model="outsourcing.outOrderNo"
              size="small"
              placeholder="请输入外协订单号"
            />
          </el-col>
        </el-form-item>
        <el-form-item label="外协单价：" prop="name">
          <el-col :span="8">
            <el-input
              v-model="outsourcing.unitPrice"
              size="small"
              placeholder="请输入外协单价"
            />
          </el-col>
        </el-form-item>
        <el-form-item label="外协单价审核：" prop="name">
          <el-col :span="8">
            <el-input
              v-model="outsourcing.unitPriceExamine"
              size="small"
              placeholder="请输入外协单价审核"
            />
          </el-col>
        </el-form-item>
        <el-form-item label="外协交期：">
          <el-col :span="6">
            <el-date-picker
              v-model="outsourcing.deliveryTime"
              value-format="yyyy-MM-dd"
              type="date"
              placeholder="选择外协交期"
            />
          </el-col>
        </el-form-item>
        <el-form-item label="外协总价：" prop="name">
          <el-col :span="8">
            <el-input
              v-model="outsourcing.price"
              size="small"
              placeholder="请输入外协总价"
            />
          </el-col>
        </el-form-item>
        <el-form-item label="是否交货：" prop="gender">
          <el-col :span="8">
            <el-select
              v-model="outsourcing.isDelivery"
              clearable
              placeholder="请选择是否交货"
            >
              <el-option
                v-for="item in valueList"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-col>
        </el-form-item>
        <el-form-item label="是否备料：" prop="gender">
          <el-col :span="8">
            <el-select
              v-model="outsourcing.isStock"
              clearable
              placeholder="请选择是否备料"
            >
              <el-option
                v-for="item in valueList"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-col>
        </el-form-item>

        <el-form-item label="是否到货：" prop="gender">
          <el-col :span="8">
            <el-select
              v-model="outsourcing.isArrival"
              clearable
              placeholder="请选择是否到货"
            >
              <el-option
                v-for="item in valueList"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-col>
        </el-form-item>
        <el-form-item label="材料外发：" prop="gender">
          <el-col :span="8">
            <el-select
              v-model="outsourcing.materialOut"
              clearable
              placeholder="请选择材料外发"
            >
              <el-option
                v-for="item in valueList"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-col>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="restData()">取 消</el-button>
        <el-button type="primary" @click="append('outsourcing')">确 定</el-button>
      </div>
    </el-dialog>
    <el-dialog
      :visible.sync="dialogDelect"
    >
      <el-input
        :rows="2"
        v-model="textDelect"
        type="textarea"
        placeholder="请输入删除理由"/>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="delectClick">确 定</el-button>
      </div>
    </el-dialog>
    <el-dialog
      :visible.sync="dialogsingle"
    >
      <el-form ref="form" :model="singleform" label-width="120px">
        <el-form-item label="选择检验人员：" prop="gender">
          <el-col :span="8">
            <el-select
              v-model="singleform.userId"
              clearable
              placeholder="请选择检验人员"
            >
              <el-option
                v-for="item in listUserP"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-col>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="outsourcingClick">确 定</el-button>
      </div>
    </el-dialog>
    <el-dialog
      :visible.sync="dialogInspection"
    >
      <el-form ref="form" label-width="120px">
        <el-form-item label="选择品检结果：" prop="gender">
          <el-col :span="8">
            <el-select
              v-model="inspectionStatue"
              clearable
              placeholder="请选择品检结果："
            >
              <el-option
                v-for="item in listInspection"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-col>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="inspectionStatueClick">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import ViewSessions from './ViewSessions.vue'
import user from '../../api/acl/user.js'
export default {
  components: {
    ViewSessions
  },
  data() {
    return {
      orderNoList: [],
      inspectionStatue: '',
      dialogInspection: false,
      listInspection: [
        {
          value: '2',
          label: '合格'
        },
        {
          value: '3',
          label: '不合格'
        }
      ],
      isinspection: !(window.sessionStorage.getItem('department') == 8),
      dialogsingle: false,
      singleform: { userId: '' },
      listUserP: [],
      dialogDelect: false,
      textDelect: '',
      total: 0, // 数据库中的总记录数
      page: 1, // 默认页码
      limit: 10, // 每页记录数
      validateRules: {
      },
      outsourcingList: [],
      dialogFormVisible: false,
      dialogFormValue: '',
      valueList: [
        {
          value: '0',
          label: '否'
        },
        {
          value: '1',
          label: '是'
        }
      ],
      outsourcing: {
        unitPriceExamine: '',
        productId: '',
        id: '',
        productCode: '', // 产品
        orderCode: '', // 订单
        orderId: '',
        manufacturer: '',
        outTime: '',
        outOrderNo: '',
        unitPrice: '',
        deliveryTime: '',
        price: '',
        isDelivery: '',
        isStock: '',
        isArrival: '',
        materialOut: ''
      },
      searchInfo: {
      }
    }
  },
  // 监听上面文本框搜索
  watch: {},

  created() {},
  mounted() {
    this.listUser()
    this.fullAssistList()
    this.listAllOrder()
    if (this.$route.query.productCode) {
      this.outsourcing.productCode = this.$route.query.productCode
    }
    if (this.$route.query.productId) {
      this.outsourcing.productId = this.$route.query.productId
    }
  },
  methods: {
    changeorder(val) {
      this.orderNoList.forEach(item => {
        if (item.insideCode == val) {
          console.log(item)
          this.outsourcing.orderId = item.id
        }
      })
    },
    listAllOrder() {
      user
        .listAllOrder()
        .then((response) => {
          this.orderNoList = response
        })
    },
    inspectionStatueClick() {
      console.log(this.inspectionStatue)
      if (!this.inspectionStatue) {
        this.$message({
          message: '请选择品检结果',
          duration: 1000
        })
      } else {
        user
          .fullAssistupdate({
            id: this.outsourcing.id,
            status: this.inspectionStatue
          })
          .then((response) => {
            this.$message({
              message: '检查成功',
              duration: 1000
            })
            this.dialogInspection = false
            this.fullAssistList()
          })
      }
    },
    inspectionClick(row) {
      this.dialogInspection = true
      this.inspectionStatue = ''
      this.outsourcing.id = row.id
    },
    outsourcingClick() {
      console.log(this.singleform.isoutsourcing)
      user
        .fullAssistupdate({
          id: this.outsourcing.id,
          userId: this.singleform.userId,
          status: 1
        })
        .then((response) => {
          this.$message({
            message: '指派成功',
            duration: 1000
          })
          this.dialogsingle = false
          this.fullAssistList()
        })
    },
    listUser() {
      user
        .listUser({
          department: '8',
          limit: 10000,
          page: 1
        })
        .then((response) => {
          console.log(response)
          for (let i = 0; i < response.list.length; i++) {
            const obj = {}
            if (response.list[i].fullName != '' && response.list[i].fullName != null && response.list[i].fullName != undefined) {
              obj.value = response.list[i].id
              obj.label = response.list[i].fullName
              this.listUserP.push(obj)
            }
          }
        })
    },
    assigned(row) {
      this.dialogsingle = true
      this.outsourcing.id = row.id
      this.singleform.userId = ''
    },
    tableRowClassName({ row, rowIndex }) {
      if (row.deleteFlag == 1) {
        return 'warning-row'
      }
      return ''
    },
    delectClick() {
      if (this.textDelect == '') {
        this.$message({
          message: '请输入删除理由',
          duration: 1000
        })
        return
      }
      user.fullAssistdelete({
        deleteFlag: 1,
        id: this.delectId,
        deleteRemark: this.textDelect
      }).then((response) => {
        this.delectId = ''
        this.textDelect = ''
        this.dialogDelect = false
        this.$message({
          message: '删除成功',
          duration: 1000
        })
        this.fullAssistList()
      })
    },
    delect(val) {
      this.delectId = val.id
      this.textDelect = ''
      this.dialogDelect = true
    },
    fetchData(val) {
      this.page = val
      this.fullAssistList()
    },
    fullAssistList() {
      user
        .fullAssistList({
          productCode: this.$route.query.productCode,
          limit: this.limit,
          page: this.page
        })
        .then((response) => {
          this.total = response.total
          this.outsourcingList = response.list
        })
    },
    closeDialog() {
      this.outsourcing = {
        unitPriceExamine: '',
        id: '',
        manufacturer: '',
        outTime: '',
        outOrderNo: '',
        unitPrice: '',
        deliveryTime: '',
        price: '',
        isDelivery: '',
        isStock: '',
        isArrival: '',
        materialOut: ''
      }
      this.$refs.outsourcing.resetFields()
    },
    append(val) {
      this.$refs[val].validate((valid) => {
        if (valid) {
          if (this.outsourcing.id == '') {
          // 新增
            user
              .fullAssist({
                unitPriceExamine: this.outsourcing.unitPriceExamine,
                orderId: this.outsourcing.orderId,
                orderCode: this.outsourcing.orderCode,
                productId: this.outsourcing.productId,
                productCode: this.outsourcing.productCode,
                manufacturer: this.outsourcing.manufacturer,
                outTime: this.outsourcing.outTime,
                outOrderNo: this.outsourcing.outOrderNo,
                unitPrice: this.outsourcing.unitPrice,
                deliveryTime: this.outsourcing.deliveryTime,
                price: this.outsourcing.price,
                isDelivery: this.outsourcing.isDelivery,
                isStock: this.outsourcing.isStock,
                isArrival: this.outsourcing.isArrival,
                materialOut: this.outsourcing.materialOut
              })
              .then((response) => {
                this.$message({
                  message: '新增成功',
                  duration: 1000
                })
                this.dialogFormVisible = false
                this.fullAssistList()
              })
          } else {
            user
              .fullAssistupdate({
                unitPriceExamine: this.outsourcing.unitPriceExamine,
                id: this.outsourcing.id,
                orderId: this.outsourcing.orderId,
                orderCode: this.outsourcing.orderCode,
                productId: this.outsourcing.productId,
                productCode: this.outsourcing.productCode,
                manufacturer: this.outsourcing.manufacturer,
                outTime: this.outsourcing.outTime,
                outOrderNo: this.outsourcing.outOrderNo,
                unitPrice: this.outsourcing.unitPrice,
                deliveryTime: this.outsourcing.deliveryTime,
                price: this.outsourcing.price,
                isDelivery: this.outsourcing.isDelivery,
                isStock: this.outsourcing.isStock,
                isArrival: this.outsourcing.isArrival,
                materialOut: this.outsourcing.materialOut
              })
              .then((response) => {
                this.$message({
                  message: '修改成功',
                  duration: 1000
                })
                this.dialogFormVisible = false
                this.fullAssistList()
              })
          }
        }
      })
    },
    restData() {
      this.dialogFormVisible = false
      this.dialogFormValue = ''
    },
    selected(val) {
      this.searchInfo = val
      this.page = 1
      this.fullAssistList()
    },
    add() {
      this.dialogFormVisible = true
      this.dialogFormValue = '新增外协表单'
    },
    upload(val) {
      this.dialogFormVisible = true
      this.dialogFormValue = '修改外协表单'
      this.outsourcing = {
        unitPriceExamine: val.unitPriceExamine,
        id: val.id,
        orderId: val.orderId,
        orderCode: val.orderCode,
        manufacturer: val.manufacturer,
        outTime: val.outTime,
        outOrderNo: val.outOrderNo,
        unitPrice: val.unitPrice,
        deliveryTime: val.deliveryTime,
        price: val.price,
        isDelivery: val.isDelivery,
        isStock: val.isStock,
        isArrival: val.isArrival,
        materialOut: val.materialOut
      }
    }
  }
}
</script>
<style>
.el-table .warning-row {
    background:#ff02027a;
  }
</style>
